#coding=utf-8
import requests,json,urllib
import mysql_password
import ID_card
import hashlib
import random
import purchaseApply,platform_login_api
#注册用户
def register(phone,pwd,username,authCode):
    try:
        s = requests.session()
        #获取加密后的密码、code、salt
        get_code = s.get('http://community-platform-api-test.gdgyun.com/api/community/account/get/verificationCode?mobile=' + phone)
        code = str(get_code.json()["data"]["code"])
        salt = str(get_code.json()["data"]["salt"])
        m = hashlib.md5()  # 创建md5对象
        m.update(pwd)  # 生成加密串，其中password是要加密的字符串,第一次加密
        pwd1 = m.hexdigest()  # 打印经过md5加密的字符串
        pwd = hashlib.md5()  # 第二次加密
        pwd.update(str(pwd1) + salt)
        list = [pwd.hexdigest(), code, salt]
        # print list[0], list[1], list[2]
        #通过id_card函数获取身份证号码
        idcard = ID_card.idcard()
        register_data = ({"mobile":str(phone),"encryptedPassword":list[0],"code":list[1],'entry':'new'})
        register = s.post('http://community-platform-api-test.gdgyun.com/api/community/account/register',json=register_data)
        #实名认证流程，成员身份可忽略此步骤，合伙人身份为必填
        authenticate_data = ({"realName":str(username),"idCardNo":idcard,"mobile":str(phone)})
        authenticate = s.post('http://community-platform-api-test.gdgyun.com/api/community/account/authenticate',json=authenticate_data)
        address = str(authenticate.json()["data"]["address"])
        birthday = str(authenticate.json()["data"]["birthday"])
        #若仅是成员身份，则authCode填写邀请码；若是合伙人身份，则authCode填写付款款
        activate_data = ({"authCode":authCode,"address":address,"birthday":birthday,"encryptedPassword":list[0],"gender":"FEMALE","idCardNo":idcard,"mobile":str(phone),"realName":str(username),"salt":list[2]})
        activate = s.post('http://community-platform-api-test.gdgyun.com/api/community/account/activate',json=activate_data)
        print register.text
        print authenticate.text
        print activate.text

    except:
        print u'异常！'



#随机生成中文姓名
def name():
    last_names = ['赵', '钱', '孙', '李', '周', '吴', '郑', '王', '冯', '陈', '褚', '卫', '蒋', '沈', '韩', '杨', '朱', '秦', '尤', '许',
                  '姚', '邵', '堪', '汪', '祁', '毛', '禹', '狄', '米', '贝', '明', '臧', '计', '伏', '成', '戴', '谈', '宋', '茅', '庞',
                  '熊', '纪', '舒', '屈', '项', '祝', '董', '梁']

    first_names = ['的', '一', '是', '了', '我', '不', '人', '在', '他', '有', '这', '个', '上', '们', '来', '到', '时', '大', '地', '为',
                   '子', '中', '你', '说', '生', '国', '年', '着', '就', '那', '和', '要', '她', '出', '也', '得', '里', '后', '自', '以',
                   '乾', '坤', '舒', '屈', '项', '祝', '董', '梁']
    num1 = random.randint(0,47)
    num2 = random.randint(0,47)
    name = last_names[num1] + first_names[num2]
    return name

def password(mobile,pwd):
    s = requests.session()
    # 获取加密后的密码、code、salt
    get_code = s.get('http://community-platform-api-test.gdgyun.com/api/community/account/get/verificationCode?mobile=' + mobile)
    msg = str(get_code.json()["msg"])
    code = str(get_code.json()["data"]["code"])
    salt = str(get_code.json()["data"]["salt"])
    m = hashlib.md5()  # 创建md5对象
    m.update(pwd)  # 生成加密串，其中password是要加密的字符串,第一次加密
    pwd1 = m.hexdigest()  # 打印经过md5加密的字符串
    pwd = hashlib.md5()  # 第二次加密
    pwd.update(str(pwd1) + salt)
    list = [pwd.hexdigest(), code, salt, msg]
    #print list
    return list

#新版注册流程
def register_v2(mobile,password):
    headers = {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'ProcessorInfo': 'A11-6', 'AppVersion': '1.0.0',
               'User-Agent': 'DuoYunCommunity/1.0.0 (iPhone; iOS 12.1.2; Scale/3.00)'
        , 'Host': 'community-platform-api-test.gdgyun.com'
        , 'System': 'iOS', 'DeviceName': 'SmileSoul', 'AppName': 'DuoYunCommunity',
               'SystemVersion': '12.1.2', 'Connection': 'keep-alive',
               'Accept-Language': 'zh-Hans-CN;q=1, en-CN;q=0.9, zh-Hant-CN;q=0.8, hu-CN;q=0.7'
        , 'ScreenResolution': '750X1624'}
    s = requests.session()
    #判断手机号是否注册
    check_mobile = s.get('http://community-platform-api-test.gdgyun.com/api/community/account/check/mobile?version=2&mobile='+mobile,headers=headers)
    check_mobile_msg = check_mobile.json()["msg"]
    check_mobile_data = str(check_mobile.json()["data"]["isPartner"])
    # print check_mobile_msg,check_mobile_data
    if check_mobile_msg == '该用户账号不存在' and check_mobile_data == 'False':   #成员注册
        # 获取加密后的密码、code、salt
        get_code = s.get('http://community-platform-api-test.gdgyun.com/api/community/account/get/verificationCode?mobile=' + mobile,headers=headers)
        code = str(get_code.json()["data"]["code"])
        salt = str(get_code.json()["data"]["salt"])
        m = hashlib.md5()  # 创建md5对象
        m.update(password)  # 生成加密串，其中password是要加密的字符串,第一次加密
        pwd1 = m.hexdigest()  # 打印经过md5加密的字符串
        pwd = hashlib.md5()  # 第二次加密
        pwd.update(str(pwd1) + salt)
        list = [pwd.hexdigest(), code, salt]
        register_data = ({"mobile": str(mobile), "encryptedPassword": list[0], "verificationCode": list[1], 'salt':salt,'authCode':'38ZG4FSR'})
        register = s.post('http://community-platform-api-test.gdgyun.com/api/community/account/activate?version=2',json=register_data,headers=headers)
        register_msg = register.json()["msg"]
        if register_msg == '已成功注册朵云空间':
            print u'已成功注册朵云空间，手机号码是：'+str(mobile)
        else:
            print u'注册失败！'
    if check_mobile_msg == '请在下一步页面进行注册' and check_mobile_data == 'True':  #合伙人注册
        paymentCode = check_mobile.json()["data"]["paymentCode"]
        # 获取加密后的密码、code、salt
        get_code = s.get('http://community-platform-api-test.gdgyun.com/api/community/account/get/verificationCode?mobile=' + mobile,headers=headers)
        code = str(get_code.json()["data"]["code"])
        salt = str(get_code.json()["data"]["salt"])
        m = hashlib.md5()  # 创建md5对象
        m.update(password)  # 生成加密串，其中password是要加密的字符串,第一次加密
        pwd1 = m.hexdigest()  # 打印经过md5加密的字符串
        pwd = hashlib.md5()  # 第二次加密
        pwd.update(str(pwd1) + salt)
        list = [pwd.hexdigest(), code, salt]
        register_data = ({"mobile": str(mobile), "encryptedPassword": list[0], "verificationCode": list[1], 'salt': salt,'authCode': paymentCode})
        register = s.post('http://community-platform-api-test.gdgyun.com/api/community/account/activate?version=2',json=register_data, headers=headers)
        register_msg = register.json()["msg"]
        if register_msg == '欢迎来到您的专属朵云空间':
            print u'欢迎来到您的专属朵云空间，手机号码是：'+str(mobile)
        else:
            print register_msg

def register_abnormal(mobile,password,productId):
    headers = {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'ProcessorInfo': 'A11-6', 'AppVersion': '1.0.0',
               'User-Agent': 'DuoYunCommunity/1.0.0 (iPhone; iOS 12.1.2; Scale/3.00)'
        , 'Host': 'community-platform-api-test.gdgyun.com'
        , 'System': 'iOS', 'DeviceName': 'SmileSoul', 'AppName': 'DuoYunCommunity',
               'SystemVersion': '12.1.2', 'Connection': 'keep-alive',
               'Accept-Language': 'zh-Hans-CN;q=1, en-CN;q=0.9, zh-Hant-CN;q=0.8, hu-CN;q=0.7'
        , 'ScreenResolution': '750X1624'}
    s = requests.session()
    # 判断手机号是否注册
    check_mobile = s.get('http://community-platform-api-test.gdgyun.com/api/community/account/check/mobile?version=2&mobile=' + mobile,headers=headers)
    check_mobile_msg = check_mobile.json()["msg"]
    if check_mobile_msg == '该用户账号已注册':
        print u'该用户账号已注册'
    try:
        check_mobile_data = str(check_mobile.json()["data"]["isPartner"])
    except:
        print u'该用户账号已注册'
    if check_mobile_msg == '该用户账号不存在' and check_mobile_data == 'False':  # 成员注册异常流程-在注册的第二个页面申请购买付款码
        #先进行合伙人购买
        username = str(name())
        Apply_code = purchaseApply.submit(mobile, username, 'PASSED', productId)[1]  # 获得合伙人付款码
        # 获取加密后的密码、code、salt
        get_code = s.get('http://community-platform-api-test.gdgyun.com/api/community/account/get/verificationCode?mobile=' + mobile,headers=headers)
        code = str(get_code.json()["data"]["code"])
        salt = str(get_code.json()["data"]["salt"])
        m = hashlib.md5()  # 创建md5对象
        m.update(password)  # 生成加密串，其中password是要加密的字符串,第一次加密
        pwd1 = m.hexdigest()  # 打印经过md5加密的字符串
        pwd = hashlib.md5()  # 第二次加密
        pwd.update(str(pwd1) + salt)
        list = [pwd.hexdigest(), code, salt]
        register_data = (
        {"mobile": str(mobile), "encryptedPassword": list[0], "verificationCode": list[1], 'salt': salt,'authCode': '38ZG4FSR'})
        register = s.post('http://community-platform-api-test.gdgyun.com/api/community/account/activate?version=2',json=register_data, headers=headers)
        register_msg = register.json()["msg"]
        if register_msg == '欢迎来到您的专属朵云空间':
            print u'欢迎来到您的专属朵云空间'
            headers = platform_login_api.login(mobile)[0]
            profile = s.get('http://community-platform-api-test.gdgyun.com/api/community/load/profile',headers=headers)
            isDualRole = profile.json()["data"]["isDualRole"]
            if isDualRole == 'false':
                print u'异常流程注册有误！该用户仅为成员身份'
            if isDualRole == 'true':
                print u'异常流程注册成功！该用户为双身份'
        else:
            print u'注册失败！'
    if check_mobile_msg == '请在下一步页面进行注册' and check_mobile_data == 'True':  #合伙人注册
        paymentCode = check_mobile.json()["data"]["paymentCode"]
        # 获取加密后的密码、code、salt
        get_code = s.get('http://community-platform-api-test.gdgyun.com/api/community/account/get/verificationCode?mobile=' + mobile,headers=headers)
        code = str(get_code.json()["data"]["code"])
        salt = str(get_code.json()["data"]["salt"])
        m = hashlib.md5()  # 创建md5对象
        m.update(password)  # 生成加密串，其中password是要加密的字符串,第一次加密
        pwd1 = m.hexdigest()  # 打印经过md5加密的字符串
        pwd = hashlib.md5()  # 第二次加密
        pwd.update(str(pwd1) + salt)
        list = [pwd.hexdigest(), code, salt]
        register_data = ({"mobile": str(mobile), "encryptedPassword": list[0], "verificationCode": list[1], 'salt': salt,'authCode': paymentCode})
        register = s.post('http://community-platform-api-test.gdgyun.com/api/community/account/activate?version=2',json=register_data, headers=headers)
        register_msg = register.json()["msg"]
        if register_msg == '欢迎来到您的专属朵云空间':
            print u'欢迎来到您的专属朵云空间'
            headers = platform_login_api.login(mobile)[0]
            #加入陈大佬的朵云
            joinCommunity = s.put('http://community-platform-api-test.gdgyun.com/api/community/joinCommunity?invitationCode=38ZG4FSR',headers=headers)
            profile = s.get('http://community-platform-api-test.gdgyun.com/api/community/load/profile', headers=headers)
            isDualRole = profile.json()["data"]["isDualRole"]
            if isDualRole == 'false':
                print u'合伙人注册有误！该用户仅为合伙人身份'
            if isDualRole == 'true':
                print u'合伙人注册成功！该用户为双身份'
        else:
            print u'注册失败！'
    else:
        print u'接口有误，请检查接口响应！'


#随机生成公司名称
def company_name():
    last_names = ['恒', '合', '智', '联', '创', '享', '投', '工', '企' ,'富', '乐', '易']
    second_name = ['宝', '迪', '捷', '驰', '众', '菲', '福', '广', '海' ,'华', '凯', '兰', '领', '摩', '迈', '讴', '思', '蔚']
    first_names = '云'
    num1 = random.randint(0,11)
    num2 = random.randint(0,17)
    name = last_names[num1] + second_name[num2] + first_names
    print name
if __name__ =='__main__':
   # for i in range(10000041,10000501,1):
   #      register('13100010053','cq123321',str(name()),purchaseApply.submit('13100010053',str(name()),'TWO','PASSED')[1])
        # print '131'+str(i)
   #register('13100010054', 'cq123321', str(name()),purchaseApply.submit('13100010054', str(name()),'PASSED','2')[1])
   #register_abnormal('13800010241','cq123321','2')
   #password('13800010196','cq123321')
   # for i in range(10000289,10001000,1):
   #     register_v2('138'+str(i), 'cq123321')
   register_v2('13800010306', 'cq123321')




